package com.jk.vehicle.mini;

import com.jk.vehicle.dynamic.annotation.EnableDynamicDataSource;
import com.jk.vehicle.mini.swagger.annotation.EnableJxstjhSwagger2;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@EnableScheduling
@EnableJxstjhSwagger2
@EnableFeignClients
@EnableDiscoveryClient
@EnableDynamicDataSource
@SpringBootApplication
@MapperScan(basePackages = {"com.jk.vehicle.*.dao"})
public class VehicleMiniCenterApplication {

    public static void main(String[] args) {
        SpringApplication.run(VehicleMiniCenterApplication.class, args);
    }

    @Bean
    public CorsFilter corsFilter() {
        System.out.println("=============== 设置跨域过滤器 ================");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        return corsConfiguration;
    }
}
